package cn.iocoder.yudao.module.handguard.controller.app.housingresource;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.handguard.controller.admin.housingresource.vo.*;
import cn.iocoder.yudao.module.handguard.controller.app.housingresource.vo.HousingAreaRespVO;
import cn.iocoder.yudao.module.handguard.convert.housingresource.HousingResourceConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.housingresource.HousingResourceDO;
import cn.iocoder.yudao.module.handguard.service.housingresource.HousingResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "小程序 - 房源信息")
@RestController
@RequestMapping("/hutai/housing-resource")
@Validated
public class HousingResourceAppController {

    @Resource
    private HousingResourceService housingResourceService;


    @GetMapping("/get")
    @Operation(summary = "获得房源信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<HousingResourceRespVO> getHousingResource(@RequestParam("id") Long id) {
        HousingResourceDO housingResource = housingResourceService.getHousingResource(id);
        HousingResourceRespVO respVO = HousingResourceConvert.INSTANCE.convert(housingResource);
        if (housingResource!=null){
            String mobile = housingResourceService.getHousingMobile();
            if (StringUtils.isNotEmpty(mobile))
                respVO.setServiceTel(mobile);

        }
        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得房源信息列表")
    @PermitAll
    public CommonResult<List<HousingResourceRespVO>> getHousingResourceList() {
        List<HousingResourceDO> list = housingResourceService.getHousingResourceList();
        return success(HousingResourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得房源信息分页")
    @PermitAll
    public CommonResult<PageResult<HousingResourceRespVO>> getHousingResourcePage(@Valid HousingResourcePageReqVO pageVO) {
        pageVO.setSource(1);
        PageResult<HousingResourceDO> pageResult = housingResourceService.getHousingResourcePage(pageVO);
        return success(HousingResourceConvert.INSTANCE.convertPage(pageResult));
    }


}
